/*
 * Copyright 2012 Niklas Rehfeld
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.madpie.ui;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JOptionPane;

/**
 *
 * @author Niklas Rehfeld
 */
public class CommandsPanel extends javax.swing.JPanel
{

    /**
     * The preferences key that holds the cpu-to-xml converter command
     */
    protected static final String K_CPU_XML_COMMAND = "cpu2xmlCommand";
    /**
     * The default cpu-to-xml converter command
     */
    protected static final String CPU_XML_DEFAULT_COMMAND = "m-lscpu";
    /**
     * The preferences key that holds the hw-to-xml converter command
     */
    protected static final String K_HW_XML_COMMAND = "hw2xmlCommand";
    /**
     * The default hw-to-xml converter command
     */
    protected static final String HW_XML_DEFAULT_COMMAND = "m-lspci";
    /**
     * The preferences key that contains whether the hw converter includes cpu
     * information.
     */
    protected static final String K_HW_INCLUDES_CPU = "hwIncludesCpu";
    /**
     * The preferences key that contains the network transfer command.
     */
    protected static final String K_NETWORK_COMMAND = "networkCommand";
    /**
     * The default network transfer command.
     */
    protected static final String NETWORK_DEFAULT_COMMAND = "m-ssh";
    /**
     * The default Disk-to-XML command.
     */
    protected static final String DISKS_DEFAULT_COMMAND = "m-fdisk";
    /**
     * The Preferences key for the disk-to-xml command.
     */
    protected static final String K_DISKS_COMMAND = "disksCommand";
    
    private static Preferences prefs;

    /**
     * Creates new form CommandsPanel
     */
    public CommandsPanel()
    {
        super();
        prefs = Preferences.userNodeForPackage(this.getClass());
        initComponents();
        cpuXmlField.setText(prefs.
                get(K_CPU_XML_COMMAND, CPU_XML_DEFAULT_COMMAND));
        hwXmlField.setText(prefs.get(K_HW_XML_COMMAND, HW_XML_DEFAULT_COMMAND));
        hwIncludesCpuCheck.setSelected(prefs.
                getBoolean(K_HW_INCLUDES_CPU, false));
        networkTransferField.setText(prefs.get(K_NETWORK_COMMAND,
                NETWORK_DEFAULT_COMMAND));
        diskXmlField.setText(prefs.get(K_DISKS_COMMAND, DISKS_DEFAULT_COMMAND));
    }

    /**
     *
     * @return The command used to convert gathered CPU metadata into the
     * Processor element of the hw.xml format. Irrelevant if hwIncludesXml()
     * returns true.
     * @see CommandsPanel#hwIncludesCpu()
     */
    public String getCpuToXmlCommand()
    {
        return cpuXmlField.getText();
    }

    /**
     *
     * @return The command used to convert gathered HW metadata into the hw.xml
     * format.
     */
    public String getHwToXmlCommand()
    {
        return hwXmlField.getText();
    }

    /**
     *
     * @return true if the command used to get hardware metadata also collects
     * metadata about the cpu. In this case no cpu-to-xml command is needed.
     */
    public boolean hwIncludesCpu()
    {
        return hwIncludesCpuCheck.isSelected();
    }

    /**
     *
     * @return the command used to transfer information over the network.
     */
    public String getNetworkTransferCommand()
    {
        return networkTransferField.getText();
    }
    
    /**
     * 
     * @return The command used to convert disk lists into disks.xml format.
     */
    public String getDiskListToXmlCommand()
    {
        return diskXmlField.getText();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        hwXmlLabel = new javax.swing.JLabel();
        hwXmlField = new javax.swing.JTextField();
        cpuXmlLabel = new javax.swing.JLabel();
        cpuXmlField = new javax.swing.JTextField();
        hwIncludesCpuCheck = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        saveButton = new javax.swing.JButton();
        networkTransferLabel = new javax.swing.JLabel();
        networkTransferField = new javax.swing.JTextField();
        diskXmlLabel = new javax.swing.JLabel();
        diskXmlField = new javax.swing.JTextField();

        hwXmlLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        hwXmlLabel.setText("Hardware to XML");

        cpuXmlLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        cpuXmlLabel.setText("CPU to XML");

        hwIncludesCpuCheck.setText("Includes CPU");
        hwIncludesCpuCheck.addChangeListener(new javax.swing.event.ChangeListener()
        {
            public void stateChanged(javax.swing.event.ChangeEvent evt)
            {
                hwIncludesCpuCheckStateChanged(evt);
            }
        });

        jLabel1.setText("Commands");

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveButtonActionPerformed(evt);
            }
        });

        networkTransferLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        networkTransferLabel.setText("Network Transfer");

        diskXmlLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        diskXmlLabel.setText("Disk list to XML");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(hwXmlLabel)
                        .addComponent(networkTransferLabel)
                        .addComponent(cpuXmlLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(diskXmlLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(diskXmlField, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addComponent(cpuXmlField)
                    .addComponent(hwXmlField, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addComponent(networkTransferField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(hwIncludesCpuCheck)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(saveButton)
                .addContainerGap(453, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jSeparator1)
                .addContainerGap(266, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(228, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(networkTransferLabel)
                        .addGap(13, 13, 13)
                        .addComponent(hwXmlLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(networkTransferField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(hwXmlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(hwIncludesCpuCheck))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cpuXmlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cpuXmlLabel))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(diskXmlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(diskXmlLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void hwIncludesCpuCheckStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_hwIncludesCpuCheckStateChanged
    {//GEN-HEADEREND:event_hwIncludesCpuCheckStateChanged
        cpuXmlLabel.setEnabled(!hwIncludesCpuCheck.isSelected());
        cpuXmlField.setEnabled(!hwIncludesCpuCheck.isSelected());
    }//GEN-LAST:event_hwIncludesCpuCheckStateChanged
    
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveButtonActionPerformed
    {//GEN-HEADEREND:event_saveButtonActionPerformed
        prefs.put(K_HW_XML_COMMAND, hwXmlField.getText());
        prefs.putBoolean(K_HW_INCLUDES_CPU, hwIncludesCpuCheck.isSelected());
        prefs.put(K_NETWORK_COMMAND, networkTransferField.getText());
        prefs.put(K_DISKS_COMMAND, diskXmlField.getText());
        //only write out cpu prefs if it's not part of HW.
        if (cpuXmlField.isEnabled())
        {
            prefs.put(K_CPU_XML_COMMAND, cpuXmlField.getText());
        }
        try
        {
            prefs.flush();
        } catch (BackingStoreException ex)
        {
            Logger.getLogger(CommandsPanel.class.getName()).
                    log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this,
                    "Couldn't store preferences.\n Error: " + ex.
                    getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_saveButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cpuXmlField;
    private javax.swing.JLabel cpuXmlLabel;
    private javax.swing.JTextField diskXmlField;
    private javax.swing.JLabel diskXmlLabel;
    private javax.swing.JCheckBox hwIncludesCpuCheck;
    private javax.swing.JTextField hwXmlField;
    private javax.swing.JLabel hwXmlLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField networkTransferField;
    private javax.swing.JLabel networkTransferLabel;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
}
